package util;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.log4j.Logger;

import taper.controller.TaperController;


public class PropertiesLoader {
	private static final Logger log = Logger.getLogger(PropertiesLoader.class);
	private static int vacuumLowerLimit;
	private static int vacuumUpperLimit;
	private static int pressureLowerLimit;
	private static int pressureUpperLimit;
	private static int coefficientLowerLimit;
	private static int coefficientUpperLimit;
	private static int polynomialLowerLimit;
	private static int polynomialUpperLimit;
	private static int queryDataMaxRange;
	private static int querySPCMaxRange;
	private static int paretoMaxRange;
	private static int tendChartMaxRange;
	private static String activeStation;
	private static String nonActiveStation;
	private static String wcsFileDir;
	private static String polynomialCSV;
	private static String taperAverageOutput;
	private static int patchInterval;
	private static int wcsInterval;
	private static int monitorInterval;
	private static int taperAdjustInterval;
	private static int taperAverageInterval;
	private static int registerReaderInterval;
	private static String plcComPort;
	private static String systemUser;
	private static String adminUser;
	private static int polyOnOff;
	
	private static PropertiesLoader instance = null;
	
	
	public static PropertiesLoader getInstance() throws Exception {
		if (instance == null) {
			instance = new PropertiesLoader();
		}

		return instance;
	}

	/**
	 * Private Constructor. Use getInstance() method to obtain an instance
	 * @throws Exception 
	 */
	private PropertiesLoader() throws Exception {
		loadProperties();
	}
	
	private void loadProperties() throws Exception {
		
		Properties prop = new Properties();
		try {
		Class clazz = this.getClass();
		ClassLoader clazzLoader = clazz.getClassLoader();
		InputStream is = clazzLoader.getResourceAsStream("TaperController.properties");
		log.info("Start read properties file");
		
			prop.load(is);
			
			activeStation = prop.getProperty("active.station");
			nonActiveStation = prop.getProperty("non.active.station");
			vacuumUpperLimit = Integer.parseInt(prop.getProperty("vacuum.upper.limit"));
			vacuumLowerLimit = Integer.parseInt(prop.getProperty("vacuum.lower.limit"));
			pressureUpperLimit = Integer.parseInt(prop.getProperty("pressure.upper.limit"));
			pressureLowerLimit = Integer.parseInt(prop.getProperty("pressure.lower.limit"));
			coefficientUpperLimit = Integer.parseInt(prop.getProperty("coefficient.upper.limit"));
			coefficientLowerLimit = Integer.parseInt(prop.getProperty("coefficient.lower.limit"));
			polynomialUpperLimit = Integer.parseInt(prop.getProperty("polynomial.upper.limit"));
			polynomialLowerLimit = Integer.parseInt(prop.getProperty("polynomial.lower.limit"));
			queryDataMaxRange = Integer.parseInt(prop.getProperty("queryData.max.range"));
			querySPCMaxRange = Integer.parseInt(prop.getProperty("querySPC.max.range"));
			paretoMaxRange = Integer.parseInt(prop.getProperty("pareto.max.range"));
			tendChartMaxRange = Integer.parseInt(prop.getProperty("tendChart.max.range"));
			wcsFileDir = prop.getProperty("wscfile.dir");
			polynomialCSV = prop.getProperty("polynomial.cvs.file");
			taperAverageOutput = prop.getProperty("taper.average.output.file");
			patchInterval =  Integer.parseInt(prop.getProperty("patch.interval"));
			wcsInterval = Integer.parseInt(prop.getProperty("wsc.interval")); 
			monitorInterval = Integer.parseInt(prop.getProperty("monitor.interval")); 
			taperAdjustInterval = Integer.parseInt(prop.getProperty("taper.adjust.interval")); 
			taperAverageInterval = Integer.parseInt(prop.getProperty("taper.average.interval")); 
			registerReaderInterval = Integer.parseInt(prop.getProperty("register.reader.interval"));
			plcComPort = prop.getProperty("plc.com.port");
			systemUser =  prop.getProperty("system.userid");
			adminUser =  prop.getProperty("admin.userid");
			polyOnOff = Integer.parseInt(prop.getProperty("polynomial.on.off"));
			
		} catch (Exception e) {
			log.error("PropertiesLoader:loadProperties",e);
			throw new Exception("Error read Taper controller Properties file",e);
		}
		
		
	}

	public static int getPressureLowerLimit() {
		return pressureLowerLimit;
	}

	public static int getPressureUpperLimit() {
		return pressureUpperLimit;
	}

	public static int getVacuumLowerLimit() {
		return vacuumLowerLimit;
	}

	public static int getVacuumUpperLimit() {
		return vacuumUpperLimit;
	}
	
	public static int getPolynomialLowerLimit() {
		return polynomialLowerLimit;
	}

	public static int getPolynomialUpperLimit() {
		return polynomialUpperLimit;
	}

	public static int getCoefficientLowerLimit() {
		return coefficientLowerLimit;
	}

	public static int getCoefficientUpperLimit() {
		return coefficientUpperLimit;
	}
	
	public static int getQueryDataMaxRange() {
		return queryDataMaxRange;
	}
	
	public static int getQuerySPCMaxRange() {
		return querySPCMaxRange;
	}
	
	public static int getParetoMaxRange() {
		return paretoMaxRange;
	}
	
	public static int getTendChartMaxRange() {
		return tendChartMaxRange;
	}
	
	public static String getActiveStation() {
		return activeStation;
	}

	public static String getNonActiveStation() {
		return nonActiveStation;
	}

	public static String getWcsFileDir() {
		return wcsFileDir;
	}

	public static String getPolynomialCSV() {
		return polynomialCSV;
	}

	public static String getTaperAverageOutput() {
		return taperAverageOutput;
	}

	public static int getPatchInterval() {
		return patchInterval;
	}

	public static int getWcsInterval() {
		return wcsInterval;
	}

	public static int getMonitorInterval() {
		return monitorInterval;
	}

	public static int getTaperAdjustInterval() {
		return taperAdjustInterval;
	}

	public static String getPlcComPort() {
		return plcComPort;
	}

	public static String getSystemUser() {
		return systemUser;
	}
	
	public static String getAdminUser() {
		return adminUser;
	}
	
	public static int getRegisterReaderInterval() {
		return registerReaderInterval;
	}

	public static int getTaperAverageInterval() {
		return taperAverageInterval;
	}

	public static int getPolyOnOff() {
		return polyOnOff;
	}

	public static void setPolyOnOff(int polyOnOff) {
		PropertiesLoader.polyOnOff = polyOnOff;
	}
	
}
